package org.flower.dao.impl;

import org.flower.dao.ShoppingCartDao;
import org.flower.entity.CommodityDetails;
import org.flower.entity.ShoppingCart;
import org.flower.utils.BaseDao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ShoppingCartDaoImpl extends BaseDao implements ShoppingCartDao {
    //根据用户编号查询购物车商品总数量
    @Override
    public int getShoppingCountNum(int uid) {
        int num = 0;
        String sql = "select SUM(num) from shoppingcart where userId=?";
        ResultSet rs = super.executeQuery(sql, uid);
        try {
            if(rs.next()){
                num = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return num;
    }

    // 根据用户编号查所有的购物车商品
    @Override
    public List<ShoppingCart> getShoppingAllByUid(int uid) {
        String sql = "select * from shoppingcart s inner join commoditydetails c on c.id=s.comDetailsId where userId=?";
        List<ShoppingCart> shoppingList = new ArrayList<ShoppingCart>();
        ResultSet rs = super.executeQuery(sql, uid);
        try {
            while(rs.next()){
                //创建购物车对象
                ShoppingCart sh = new ShoppingCart();
                sh.setId(rs.getInt("s.id"));
                sh.setComDetailsId(rs.getInt("comDetailsId"));
                sh.setUserId(rs.getInt("userId"));
                sh.setNum(rs.getInt("num"));
                sh.setFlag(rs.getBoolean("flag"));
                //创建商品对象
                CommodityDetails com = new CommodityDetails(rs.getInt("c.id"),rs.getString("ComName"),
                        rs.getString("Content"),rs.getInt("outNum"),
                        rs.getDouble("outPrice"),rs.getDouble("marketPrice"),
                        rs.getString("MaterialScience"),rs.getString("packing"),
                        rs.getString("Distribution"),rs.getString("comImage"),
                        rs.getString("detailsImage"),rs.getInt("comShowId"),
                        rs.getInt("ClassId"),rs.getInt("stockd"),
                        rs.getDate("insertDate"));
                //处理商品图片
                String ress[]=com.getComImage().split(",");
                com.setComImage(ress[0]);
                //将商品信息 添加到 购物车中
                sh.setCommodity(com);
                //添加到购物车集合
                shoppingList.add(sh);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return shoppingList;
    }
    //添加购物车
    @Override
    public boolean addShoppingCart(ShoppingCart shop) {
        String sql = "insert into shoppingcart values(null,?,?,1,true)";
        //执行添加
        return super.executeUpdate(sql,shop.getComDetailsId(),shop.getUserId());
    }
    // 根据用户编号和商品编号查询该商品是否添加过
    @Override
    public boolean getisExistByComIdAndUid(int uid, int comid) {
        String sql = "select * from  shoppingcart where userId=? and ComdetailsId=?";
        boolean flag = false;
        //如果可以查到 就说明 该用户的该商品添加过 就返回 true
        ResultSet rs = super.executeQuery(sql, uid,comid);
        try {
            if(rs.next()){
                flag = true;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return flag;
    }
    //根据用户编号和商品编号 修改商品的数量
    @Override
    public boolean updateShoppingNum(int uid, int comid, int num) {
        String sql = "update shoppingcart set num = num+? where userId=? and ComdetailsId=?";
        return super.executeUpdate(sql,num,uid,comid);
    }
    //根据编号删除购物车信息
    @Override
    public boolean delShoppingById(int id) {
        String sql = "delete from shoppingcart where id =?";
        return super.executeUpdate(sql,id);
    }
    //根据编号修改购物车状态
    @Override
    public boolean updateShoppingFlag(int id) {
        String sql = "update shoppingcart set flag = !flag where id=?";
        return super.executeUpdate(sql,id);
    }
    // 根据id和 修改购物车数量 input文本框输入
    @Override
    public boolean updateShoppingNumInput(int id, int num) {
        String sql = "update shoppingcart set num=? where id=?";
        return super.executeUpdate(sql,num,id);
    }
    //根据编号修改商品数量 商品数量+1或者-1
    @Override
    public boolean updateShoppingNum1(int id, int num) {
        String sql = "update shoppingcart set num = num+? where id=?";
        return super.executeUpdate(sql,num,id);
    }

    @Override
    public List<ShoppingCart> getShoppingAllAndYesChangedByUid(int uid) {
        //获取购物车 勾选的商品信息
        String sql = "select * from shoppingcart s inner join commoditydetails c on c.id=s.comDetailsId where s.flag=true and userId=?";
        List<ShoppingCart> shoppingList = new ArrayList<ShoppingCart>();
        ResultSet rs = super.executeQuery(sql, uid);
        try {
            while(rs.next()){
                //创建购物车对象
                ShoppingCart sh = new ShoppingCart();
                sh.setId(rs.getInt("s.id"));
                sh.setComDetailsId(rs.getInt("comDetailsId"));
                sh.setUserId(rs.getInt("userId"));
                sh.setNum(rs.getInt("num"));
                sh.setFlag(rs.getBoolean("flag"));
                //创建商品对象
                CommodityDetails com = new CommodityDetails(rs.getInt("c.id"),rs.getString("ComName"),
                        rs.getString("Content"),rs.getInt("outNum"),
                        rs.getDouble("outPrice"),rs.getDouble("marketPrice"),
                        rs.getString("MaterialScience"),rs.getString("packing"),
                        rs.getString("Distribution"),rs.getString("comImage"),
                        rs.getString("detailsImage"),rs.getInt("comShowId"),
                        rs.getInt("ClassId"),rs.getInt("stockd"),
                        rs.getDate("insertDate"));
                //处理商品图片
                String ress[]=com.getComImage().split(",");
                com.setComImage(ress[0]);
                //将商品信息 添加到 购物车中
                sh.setCommodity(com);
                //添加到购物车集合
                shoppingList.add(sh);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return shoppingList;
    }
}
